<?php

function get_best_white_move($moves)
{
	$best_score = -999999999;
	$best_move = null;
	
	shuffle($moves);
	
	foreach($moves as $move)
	{
		$score = basic_score($move);
		if($score > $best_score)
		{
			$best_score = $score;
			$best_move = $move;
		}
	}
	return($best_move);
}

function get_best_black_move($moves)
{
	$best_score = 999999999;
	$best_move = null;
	
	shuffle($moves);
	
	foreach($moves as $move)
	{
		$score = basic_score($move);
		if($score < $best_score)
		{
			$best_score = $score;
			$best_move = $move;
		}
	}
	return($best_move);
}

/*
score as in standard chess, with 
100 per pawn,
300 for its Bishop or Knight, 
500 for its Rook, and 
900 for its Queen. 
1800 for its King
Take the difference between the two sides to get the score.
*/
function basic_score($board)
{
	$white_score = 0;
	$black_score = 0;
	
	for($i=0;$i<strlen($board);$i++)
	{
		switch($board[$i])
		{
			case 'k': $black_score += 1800; break;
			case 'q': $black_score += 900; break;
			case 'b': $black_score += 300; break;
			case 'n': $black_score += 300; break;
			case 'r': $black_score += 500; break;
			case 'p': $black_score += 100; break;
			case 'K': $white_score += 1800; break;
			case 'Q': $white_score += 900; break;
			case 'B': $white_score += 300; break;
			case 'N': $white_score += 300; break;
			case 'R': $white_score += 500; break;
			case 'P': $white_score += 100; break;
		}
	}
	
	return($white_score - $black_score);
}


function get_best_negamax_white_move($moves)
{
/*

To find the negamax value of state s 
with max depth d and possibly from 
the top leaving a best move in 
the global variable m0 : 
    if s is a final state or d = 0 
        return score(s) 
    M ? legal moves from s 
    extract some move m from M 
    s' ? m(s) 
    v' ? -(negamax(s', d - 1, false)) 
    if top 
        m0 ? m 
    for m in remaining moves of M 
        s' ? m(s) 
        v ? -(negamax(s', d - 1, false)) 
        if v > v' 
            v' ? v 
            if top 
                m0 ? m 
    return v'

*/

}



?>